﻿// Copyright 2009 Mike Geise
// 
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// 
//     http://www.apache.org/licenses/LICENSE-2.0
// 
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

namespace Falcon.Core.Controllers
{
	using System.Web.Mvc;
	using Falcon.Core.Domain;
	using Falcon.Core.Domain.Services;
	using Falcon.Core.Domain.Values;
	using Falcon.Core.Extensions;
	using Falcon.Core.Validation;

	[Authorize]
	public class SystemUserRoleController : DefaultController
	{
		public SystemUserRoleController(IUserRoleService userRoleService)
		{
			this.UserRoleService = userRoleService;
		}

		public IUserRoleService UserRoleService
		{
			get;
			private set;
		}

		[AcceptVerbs(HttpVerbs.Get)]
		public ActionResult Index()
		{
			var roles = UserRoleService.GetAll();

			var role = roles.FirstOrDefault();

			return role.CanView ? View(Views.Index, Masters.System, roles) : NotAuthorized();
		}

		[AcceptVerbs(HttpVerbs.Get)]
		public ActionResult Create()
		{
			var role = UserRoleService.Create();

			return role.CanCreate ? View(Views.Create, Masters.System, new UserRoleCreate()) : NotAuthorized();
		}

		[AcceptVerbs(HttpVerbs.Post)]
		public ActionResult Create(UserRoleCreate value)
		{
			try
			{
				var role = UserRoleService.Create();

				if (!role.CanCreate)
				{
					return NotAuthorized();
				}

				this.UserRoleService.Insert(role, value);

				return Message(Routes.SystemUserRoleUpdate, new { id = role.Id }, Resources.Messages.UserRoleCreated);
			}
			catch (ValidationException exception)
			{
				ModelState.CopyValidationExceptions(exception);
			}

			return View(Views.Create, Masters.System, value);
		}

		[AcceptVerbs(HttpVerbs.Get)]
		public ActionResult Update(int id)
		{
			var role = UserRoleService.GetById(id);

			if (role == null)
			{
				return Error(Resources.Messages.UserRoleNotFound);
			}

			return role.CanUpdate ? View(Views.Update, Masters.System, new UserRoleUpdate(role)) : NotAuthorized();
		}

		[AcceptVerbs(HttpVerbs.Post)]
		public ActionResult Update(UserRoleUpdate value)
		{
			try
			{
				var role = UserRoleService.GetById(value.Id);

				if (role == null)
				{
					return Error(Resources.Messages.UserRoleNotFound);
				}

				if (!role.CanUpdate)
				{
					return NotAuthorized();
				}

				this.UserRoleService.Update(role, value);

				return Message(Routes.SystemUserRoleUpdate, new { id = role.Id }, Resources.Messages.UserRoleUpdated);
			}
			catch (ValidationException exception)
			{
				ModelState.CopyValidationExceptions(exception);
			}

			return View(Views.Update, Masters.System, value);
		}

		[AcceptVerbs(HttpVerbs.Get)]
		public ActionResult Delete(int id)
		{
			var role = this.UserRoleService.GetById(id);

			if (role == null)
			{
				return base.Error(Resources.Messages.UserRoleNotFound);
			}

			return role.CanDelete ? base.View(Views.Delete, Masters.System, new UserRoleDelete(role)) : NotAuthorized();
		}

		[AcceptVerbs(HttpVerbs.Post)]
		public ActionResult Delete(UserRoleDelete value)
		{
			try
			{
				var role = this.UserRoleService.GetById(value.Id);

				if (role == null)
				{
					return base.Error(Resources.Messages.CountryNotFound);
				}

				if (!role.CanDelete)
				{
					return NotAuthorized();
				}

				this.UserRoleService.Delete(role);

				return base.RedirectToRoute(Routes.SystemUserRoleIndex);
			}
			catch (ValidationException exception)
			{
				base.ModelState.CopyValidationExceptions(exception);
			}

			return base.View(Views.Delete, Masters.System, value);
		}
	}
}